package com.pzh.guli.service.user.service.imp;

import cn.pzh.guli.service.utils.JwtInfo;
import cn.pzh.guli.service.utils.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pzh.guli.service.base.handler.HandlerException;
import com.pzh.guli.service.base.result.ResultCodeEnum;
import com.pzh.guli.service.user.pojo.Member;
import com.pzh.guli.service.user.mapper.MemberMapper;
import com.pzh.guli.service.user.pojo.vo.LoginVo;
import com.pzh.guli.service.user.pojo.vo.MemberVo;
import com.pzh.guli.service.user.service.IMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xzh
 * @since 2022-07-19
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {
    @Resource
    private MemberMapper memberMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void register(MemberVo memberVo) throws HandlerException {
        QueryWrapper<Member> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("nickname",memberVo.getNickname());
        Long count = memberMapper.selectCount(queryWrapper);
        if (count!=0){ //重名
            throw new HandlerException(ResultCodeEnum.USER_NAME_EXIST);
        }
        //校验验证码
        String code=(String)redisTemplate.opsForValue().get(memberVo.getMobile());
        if (!memberVo.getCode().equals(code)){
            throw new HandlerException(ResultCodeEnum.TEL_CODE_CHECKOUT);
        }
        Member member=new Member();
        member.setNickname(memberVo.getNickname());
        member.setPassword(memberVo.getPassword());
        member.setMobile(memberVo.getMobile());
        member.setAvatar("头像");//默认头像
        memberMapper.insert(member);
    }
    //用户登录模块
    @Override
    public String login(LoginVo loginVo) throws HandlerException {
        //手机号校验
        QueryWrapper<Member> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("mobile",loginVo.getMobile());
        Member member = memberMapper.selectOne(queryWrapper);
        if (member==null){ //用户不存在
            throw new HandlerException(ResultCodeEnum.USER_IS_NOT_EXIST);
        }
        //密码校验
        if (!member.getPassword().equals(loginVo.getPassword())){
            throw new HandlerException(ResultCodeEnum.PASSWORD_ERROR);
        }
        if (member.getIsDisable()){ //禁用用户
            throw new HandlerException(ResultCodeEnum.USER_IS_DISABLE);
        }
        //登录成功 生成token
        JwtInfo jwtInfo=new JwtInfo();
        jwtInfo.setId(member.getId());
        jwtInfo.setNickname(member.getNickname());
        jwtInfo.setAvatar(member.getAvatar());
        return JwtUtil.getToken(jwtInfo, 3600);
    }
   //管理员统计每天注册人数
    @Override
    public Integer countMemberNumByDay(String day) {
        return memberMapper.selectRegisterNumByDay(day);
    }
}
